<template>
  <div class="flex flex-col h-full">
    <SearchContainer show-export @reset="() => formRef.resetFields()" @search="getData()" @export="exportRef.$el.querySelector('button').click()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="手术日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="flex-1">
      <a-space ref="exportRef" class="hidden">
        <BaseExport type="报表" name="手术工作量统计" code="001117" is-show-time :search-params="formState">导出</BaseExport>
      </a-space>
      <BaseTable column-code="001117" :loading="loading" :data-source="tableData" bordered fill :pagination="pagination" :extra-height="40">
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>合计</a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>{{ totalNumber }}</a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const exportRef = ref(null)
const formRef = ref(null)
const totalNumber = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const { loading, tableData, getData, pagination } = useTable({
  url: '/statistic/getOperatingRoomWorkloadStatistics',
  params: formState.value,
  immediate: true,
  formatParams: (params) => {
    return {
      stdate: params.dateRange?.[0],
      eddate: params.dateRange?.[1]
    }
  },
  formatResult: (result) => {
    totalNumber.value = result[0]?.zsl
    return result
  }
})
</script>
